package com.ktjiaoyu.service.impl;

import com.ktjiaoyu.entity.SysRole;
import com.ktjiaoyu.mapper.SysRoleMapper;
import com.ktjiaoyu.service.SysRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService {
    private static final Logger logger = LoggerFactory.getLogger(SysRoleServiceImpl.class);

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public List<SysRole> findAll() throws Exception {
        try{
            return sysRoleMapper.getAll();
        } catch (Exception e) {
            logger.error("执行SysRoleServiceImpl.findAll()方法出现异常", e);
            throw e;
        }
    }

    @Override
    public boolean upInsert(SysRole sysRole) throws Exception {
        try{
            if (sysRole.getId() == null) {
                // 新增角色
                return sysRoleMapper.insert(sysRole) > 0;
            }else{
                // 修改角色
                return sysRoleMapper.update(sysRole) > 0;
            }
        } catch (Exception e) {
            logger.error("执行SysRoleServiceImpl.upInsert()方法出现异常", e);
            throw e;
        }
    }


    @Override
    public boolean delete(Long id) {
        try{
            return sysRoleMapper.delete(id) > 0;
        }catch (Exception e){
            logger.error("执行SysRoleServiceImpl.delete(Long)方法出现异常。", e);
            throw e;
        }
    }

    @Override
    public SysRole bian(String code) {
        try{
            return sysRoleMapper.bian(code);
        }catch (Exception e){
            logger.error("执行SysRoleServiceImpl.bian(String)方法出现异常。", e);
            throw e;
        }
    }

    @Override
    public SysRole getById(Long id) {
        try{
            return sysRoleMapper.getById(id);
        }catch (Exception e){
            logger.error("执行SysRoleServiceImpl.getById(Long)方法出现异常。", e);
            throw e;
        }
    }
}
